Your Universal Remote Control Center
RemoteCentral.com
Philips Pronto Professional Forum - View Post
Previous section Next section Up level
Up level
The following page was printed from RemoteCentral.com:

Login:
Pass:
 
 

Topic:
Introduction and FTP example
This thread has 3 replies. Displaying all posts.
Post 1 made on Saturday January 19, 2008 at 17:41
brettd
Lurking Member
Joined:
Posts:
January 2008
6
Hi all,

I've been a long time lurker here (from the days of my RU940) and thought it was time to say hello, and thanks for all the help you have given me over the years.

I have recently upgraded to a TSU9600, and would like to share (and suggestions are welcome) an ftp example. I haven't seen anyone else try it yet.

This is the code from my weather page. It started off from the widely available "meteo" example, but has been completely re-written to get the more accurate data (for me) from the BOM. Also not the improved weather.com XML parsing.



//---------------------------------
// BOM
SetInfo ("Connecting to server . . .");
var sBOMData = ""; // Holds the incomming data
// The socket we initiate the ftp connection with.
var ftpSock = new TCPSocket(false);

// FTP data will be returned to us through this socket.
var dataSock = new TCPSocket(false);

// ftp.bom - use IP as DNS doesn't seem to work for me.
ftpSock.connect('134.178.63.130', 21, 3000);

//---------------------------------
// Weather.com
var xmlData = ""; // Holds the incomming data
var httpSock = new TCPSocket(false);
// xml.weather.com - use IP as DNS doesn't seem to work for me.
httpSock.connect('65.207.183.49', 80, 3000);
// <-- End of script!
/////////////////////////////////////////


////////////////////////////////////////////////////////////////////
// We don't need to do anything on connection.
ftpSock.onConnect = function() {} // System.print("ftpSock connected."); }
dataSock.onConnect = function() {} // System.print("dataSock connected."); }

////////////////////////////////////////////////////////////////////
// callback to show the socket is closed.
ftpSock.onClose = function() { System.print("ftpSock closed."); }

////////////////////////////////////////////////////////////////////
// The Data socket is closed automatically by the remote host
// when the transfer is complete.
dataSock.onClose = function()
{
//System.print("data socket closed. res is len: " + sBOMData.length );
//System.print( sBOMData );
ftpSock.close(); // This is all we wanted, so close the ftp socket as well.
ParseAndDisplayFile(sBOMData);
SetInfo(""); // Success!
}
////////////////////////////////////////////////////////////////////
// Data collection for the data channel
dataSock.onData = function()
{
var pkt = dataSock.read(5000,3000);
//System.print("data Recieved " + pkt.length + " bytes: " + pkt );
sBOMData += pkt;
}
////////////////////////////////////////////////////////////////////
// Data collection & error handler functions.
ftpSock.onData = function()
{
var pkt = ftpSock.read(5000,3000);
var status = HandleFtpPacket(pkt, "/anon/gen/fwo/IDA00003.dat");
SetInfo(status);
}
////////////////////////////////////////////////////////////////////
// Error handlers
ftpSock.onIOError = function(e)
{
System.print("ftpSock Error: " + e);
SetInfo ("Failed to find forecast data!");
}
dataSock.onIOError = function(e) { System.print("dataSock Error: " + e); }
////////////////////////////////////////////////////////////////////
// Sets the info label in the middle of the display.
function SetInfo( sText )
{
CF.widget("WaitLabel").label = sText;
CF.widget("WaitLabel").visible = (sText == "") ? false : true;
}

////////////////////////////////////////////////////////////////////
// A simple state machine that handles the ftp packets
// with the objective of eventually downloading sFile.
// returns a string identifying the current state.
function HandleFtpPacket( pkt, sFile )
{
var sState = "Unknown";
var ftpCode = pkt.substring(0,3); // The 1st 3 bytes are the FTP response ID.
switch (ftpCode)
{
case "220" : // Welcome message.
ftpSock.write("USER anonymous\r\n");
sState = "Logging in";
break;
case "331" : // Password request.
ftpSock.write("PASS [email protected]\r\n");
sState = "Authenticating";
break;
case "230" : // Login succeeded.
ftpSock.write("SYST\r\n");
sState = "Waiting for server";
break;
case "215" : // SYST response (server type, ie UNIX Type: L8)
ftpSock.write("PWD\r\n");
sState = "Requesting Current working folder";
break;
case "257" : // PWD response (we expect "\"
ftpSock.write("TYPE I\r\n");
sState = "Requesting binary mode";
break;
case "200" : // Changed to Binary mode
ftpSock.write("PASV\r\n");
sState = "Requesting passive mode";
break;
case "227" : // Entering passive mode.
// Open data port now ...
var ipPort = ParseFtpPassivePacket(pkt);
dataSock.connect(ipPort[0], ipPort[1], 3000);
//System.delay(250); // hacky but it does the job
sState = "Opening data port";
ftpSock.write("SIZE " + sFile + "\r\n");
break;
case "213" : //
ftpSock.write("RETR " + sFile + "\r\n");
sState = "Requesting File";
break;
case "150" : // binary data mode opening
//System.print( "OK... here comes our file!" );
sState = "Retrieving data ...";
break;
case "226" : // File send ok!
sState = "Data recieved OK";
break;
default: // Unknown packet type...
System.print("ftpSock bad packet (" + pkt.length + " bytes): " + pkt );
break;
}
return sState;
}

////////////////////////////////////////////////////////////////////
// Parses the FTP passive mode response.
function ParseFtpPassivePacket( pkt )
{
var s = pkt.substring(pkt.indexOf("(")+1, pkt.indexOf(")"));
var a = s.split(",");
var res = new Array();
res[0] = a[0] + "." + a[1] + "." + a[2] + "." + a[3];
res[1] = String( ((parseInt(a[4]) << 8) + (parseInt(a[5]))));
return res;
}
////////////////////////////////////////////////////////////////////
// Parses the BOM text file, and shows what data we have.
function ParseAndDisplayFile( src )
{
var sLines = src.split("\n");
var nCity = 1; // Skip the first row as is a header (col titles)
//System.print (sLines[0]);
while (sLines[nCity] != null)
{
var cols = sLines[nCity].split("#");
var sCity = cols[1];
if (cols[0] == "086071") // Melbourne
{
for (nDay = 0; nDay < 5; nDay++)
{
var dayName = GetDayStr( cols[3], nDay);
var summary = cols[22+nDay];
var hi = cols[7 + (nDay*2)];
var lo = cols[8 + (nDay*2)];
var icon = ChooseIcon(summary, hi);

//var out = dayName + ": " + summary;
//out += " Hi: " + hi;
//out += " Lo: " + lo;
//System.print (" " + out + " (icon: " + icon + ")");

CF.widget("day"+nDay).label = dayName;
CF.widget("hi" +nDay).label = hi;
CF.widget("lo" +nDay).label = lo;
CF.widget("dsc"+nDay).label = summary;

// Picture
var srcImage = CF.widget( icon,"BOM_RES" );
CF.widget("im"+nDay).setImage( srcImage.getImage() );
}
}
nCity++;
}
}
////////////////////////////////////////////////////////////////////
// Works out which icon to show
function ChooseIcon(summary, hi)
{
summary = summary.toLowerCase();
var iconName = "NA";

var isFine = (-1 == summary.indexOf("fine")) ? 0 : 1;
var isDry = (-1 == summary.indexOf("dry")) ? 0 : 1;
var isHot = (-1 == summary.indexOf("hot")) ? 0 : 1;
var isCloud = (-1 == summary.indexOf("cloud")) ? 0 : 1;
var isChange = (-1 == summary.indexOf("change")) ? 0 : 1;
var isClearing = (-1 == summary.indexOf("clearing")) ? 0 : 1;
var isShower = (-1 == summary.indexOf("shower")) ? 0 : 1;
var isRain = (-1 == summary.indexOf("rain")) ? 0 : 1;
var isDrizzle = (-1 == summary.indexOf("drizzle")) ? 0 : 1;
var isStorm = (-1 == summary.indexOf("storm")) ? 0 : 1;
var isThunder = (-1 == summary.indexOf("thunder")) ? 0 : 1;
var isSnow = (-1 == summary.indexOf("snow")) ? 0 : 1;

if (isSnow)
{
iconName = "Snow";
return iconName;
}
if (isThunder || isStorm)
{
iconName = "Storm";
if (isFine || isDry || isHot || isClearing)
iconName += "Sun";
return iconName;
}
if (isShower || isDrizzle || isRain)
{
iconName = "Rain";
if (isClearing || isFine || isChange || isDry || isHot)
{
iconName += "Sun";
}
else if (isShower)
{
iconName = "Shower";
}
return iconName;
}
if (isCloud)
{
iconName = "Cloud";
if (isClearing || isFine || isChange || isDry || isHot)
{
iconName += "Sun";
}
return iconName;
}
if (isClearing)
{
iconName = "Clearing";
return iconName;
}
iconName = (hi >= 33) ? "Hot" : "Sun";
return iconName;
}
////////////////////////////////////////////////////////////////////
// Identifies the day name
function GetDayStr( date, nAddDays )
{
var tmp = new Date();
tmp.setYear(date.substring(0,4));
tmp.setMonth(date.substring(4,6)-1);
tmp.setDate(date.substring(6,8));
switch ((tmp.getDay() + nAddDays) % 7)
{
case 0: return "Sunday";
case 1: return "Monday";
case 2: return "Tueday";
case 3: return "Wednesday";
case 4: return "Thursday";
case 5: return "Friday";
case 6: return "Saturday";
default: return "";
}
return "";
}

//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
// Weather.COM Methods
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////
// Once connection is established, send the request
httpSock.onConnect = function()
{
areaCode =
var sGetReq = "GET /weather/local/";
sGetReq += CF.widget("City_Code","PARAMETERS").label;
sGetReq += "?cc=*&dayf=5&ut=c&ud=k&us=k&up=m";
sGetReq += "&ur=m&prod=bd_select&par=yahoowidgetxml";
sGetReq +="HTTP/1.0 \r\n";
httpSock.write(sGetReq);
httpSock.write("HOST:xml.weather.com \r\n\r\n");
}
////////////////////////////////////////////////////////////////////
// When HTTP request is complete, the connection is closed.
// so take what we need from the xmlData.
httpSock.onClose = function()
{
if (xmlData.indexOf("= 0) // Trim the XML tag off if it was included.
xmlData = xmlData.substring(xmlData.indexOf("?>")+2);

var weather = new XML(xmlData); // ECMAScript (E4X) compatible object.
CF.widget("Temp" ).label = weather.cc.tmp;
CF.widget("Pressure").label = weather.cc.bar.r;
CF.widget("Wind" ).label = weather.cc.wind.t + " " + weather.cc.wind.s + " km/h";
CF.widget("Humidity").label = weather.cc.hmid;
CF.widget("Sunrise" ).label = weather.loc.sunr;
CF.widget("Sunset" ).label = weather.loc.suns;
}

////////////////////////////////////////////////////////////////////
// Data collection & error handler functions.
httpSock.onData = function() { xmlData += httpSock.read(9000,3000); }
httpSock.onIOError = function(e) { System.print("httpSock Error: " + e); }

Last edited by brettd on January 20, 2008 18:24.
Post 2 made on Sunday January 20, 2008 at 12:00
Quagmire9400
Lurking Member
Joined:
Posts:
January 2008
1
Does your ECMAScript code correctly parse the XML from the weather.com feed? Mine wouldn't work until I added "text()" to the end of the node specifiers (e.g. weather.cc.tmp -> weather.cc.tmp.text())
- Giggity -
OP | Post 3 made on Sunday January 20, 2008 at 18:39
brettd
Lurking Member
Joined:
Posts:
January 2008
6
Hi,

Yes the above code works for me. The only thing I had to do was trim the first part of the returned data. As the ECMAScript didn't seem to like the header:

Weather.COM provides a header like this:

<?xml version="1.0" encoding="ISO-8859-1"?>

So I excluded it before making the XML object:
    if (xmlData.indexOf("= 0)  // Trim the XML tag off if it was included.
xmlData = xmlData.substring(xmlData.indexOf("?>")+2);
var weather = new XML(result); // Make ECMAScript object

Were you assigning the value straight into a widget label like I am?

Brett
Post 4 made on Sunday June 15, 2008 at 04:20
badonj001
Lurking Member
Joined:
Posts:
June 2008
3
brettd,

if you wouldn't mind can you upload an .xcf of your Weather page? I would appreciate it.


Thanks,
Jason


Jump to


Protected Feature Before you can reply to a message...
You must first register for a Remote Central user account - it's fast and free! Or, if you already have an account, please login now.

Please read the following: Unsolicited commercial advertisements are absolutely not permitted on this forum. Other private buy & sell messages should be posted to our Marketplace. For information on how to advertise your service or product click here. Remote Central reserves the right to remove or modify any post that is deemed inappropriate.

Hosting Services by ipHouse